package com.tanhua.server.service.Impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.utils.DozerUtil;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.BlackList;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author lifeihu
 * @date 2021/12/25
 * @TIME:17:10
 */
@Service
public class SettingsServiceImpl implements SettingsService {
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    BlackListApi blackListApi;
    @Override
    public SettingsVo settings() {
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();
        Settings settings = settingsApi.settings(userId);
        SettingsVo vo = DozerUtil.map(settings, SettingsVo.class);
        vo.setId(userId);
        vo.setPhone(mobile);
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        return vo;
    }

    @Override
    public void saveQuestion(String content) {
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //2.根据用户id查询问题
        Question question = questionApi.findByUserId(userId);
        //3.如果问题为空，设置问题
        if(question==null){
        question.setUserId(userId);
        question.setTxt(content);
        questionApi.saveQuestion(question);
        }
        //4.如果问题不为空，更新问题
        question.setTxt(content);
        questionApi.update(question);
    }

    @Override
    public void saveSettings(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //2.根据用户id查询通知
        Settings settings = settingsApi.settings(userId);
        //3.如果通知为空，设置通知
        if(settings==null){
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.saveSettings(settings);
        }
        //4.如果通知不为空，修改通知
        settings.setLikeNotification(likeNotification);
        settings.setPinglunNotification(pinglunNotification);
        settings.setGonggaoNotification(gonggaoNotification);
        settingsApi.update(settings);
    }

    @Override
    public PageResult blacklist(int page, int size) {
        Long userId = UserHolder.getUserId();
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);
        //3、对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = PageResult.init(page, size, iPage.getTotal(), iPage.getRecords());
        return pr;
    }

    @Override
    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.removeById(userId,blackUserId);
    }

}
